<?php

/* *
 * 资讯
 */
namespace Common\Model;
use Think\Model;
class WorkorderModel extends BaseModel {

    public function queryForList($page=null, $map=null, $map2=null) {
        if($map["wo.workorder_type"] == 1){
            $examBasic = "left join __EXAM_BASIC__ eb on eb.exam_basic_id = wo.exam_basic_id";
            $examBasicCP = "left join __EXAM_BASIC_CP__ ebc on ebc.exam_basic_cp_id = wo.exam_basic_cp_id";
            $examMeasure = "left join __EXAM_MEASURE__ em on em.exam_measure_id = wo.exam_measure_id";
            $rx = "left join __RX__ rx on rx.rx_id = wo.rx_id";
            $field = "wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, creator.admin_name as creator, sales.admin_name as sales, examinator.admin_name as examinator, prescriber.admin_name as prescriber, reviewer.admin_name as reviewer, auditor.admin_name as auditor, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, ebc.completed as exam_basic_cp_completed, ebc.update_time as exam_basic_cp_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time";
        }else if($map["wo.workorder_type"] == 2){
            $examBasic = "left join __EXAM_BASIC_SPINE__ eb on eb.exam_basic_spine_id = wo.exam_basic_spine_id";
            $examMeasure = "left join __EXAM_MEASURE_SPINE__ em on em.exam_measure_spine_id = wo.exam_measure_spine_id";
            $field = "wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, creator.admin_name as creator, sales.admin_name as sales, examinator.admin_name as examinator, prescriber.admin_name as prescriber, reviewer.admin_name as reviewer, auditor.admin_name as auditor, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time, em.completed2 as exam_measure_completed2, em.update_time2 as exam_measure_update_time2";
        }else if($map["wo.workorder_type"] == 3) {
            $examBasic = "left join __EXAM_BASIC_CP__ eb on eb.exam_basic_cp_id = wo.exam_basic_cp_id";
            $field = "wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, creator.admin_name as creator, sales.admin_name as sales, examinator.admin_name as examinator, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time";
        }else {
            $examBasic = "left join __EXAM_BASIC_COMMON__ ebc on ebc.exam_basic_common_id = wo.exam_basic_id";
            $examMeasure = "left join __EXAM_MEASURE_COMMON__ emc on emc.exam_measure_common_id = wo.exam_measure_id";
            $field = "wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, creator.admin_name as creator, sales.admin_name as sales, examinator.admin_name as examinator, prescriber.admin_name as prescriber, reviewer.admin_name as reviewer, auditor.admin_name as auditor, pt.completed as patient_completed, ebc.completed as exam_basic_completed, ebc.update_time as exam_basic_update_time, emc.completed as exam_measure_completed, emc.update_time as exam_measure_update_time";
        }
        if( $map2 != null){
            $list = $this->alias("wo")
                ->join("left join __PATIENT__ pt on pt.patient_id = wo.patient_id")
                ->join($examBasic)
                ->join($examBasicCP)
                ->join($examMeasure)
                ->join($rx)
//            ->join("left join __EXAM_ANALYSIS__ ea on ea.exam_analysis_id = wo.exam_analysis_id")
                ->join("left join __ADMIN__ creator on creator.employee_id = wo.creator_id")
                ->join("left join __ADMIN__ sales on sales.employee_id = wo.sales_id")
                ->join("left join __ADMIN__ examinator on examinator.employee_id = wo.examinator_id")
                ->join("left join __ADMIN__ prescriber on prescriber.employee_id = wo.prescriber_id")
                ->join("left join __ADMIN__ reviewer on reviewer.employee_id = wo.reviewer_id")
                ->join("left join __ADMIN__ auditor on auditor.employee_id = wo.auditor_id")
//                ->join("left join __ADMIN__ ap on ap.user_id = wo.processing_id")
//                ->join("left join __ADMIN__ ad on ad.user_id = wo.delivery_id")
//            ->join("left join __PAY__ pay on pay.pay_id = wo.pay_id")
                ->where(getWhereSqlByMap($map))
                ->where($map2)
                ->order("wo.create_time desc")
                ->limit($page->firstRow . ',' . $page->listRows)
//            ->field("wo.*,cus.nickname,p.product_name,cus.doctor_name,cus.hospital_name,pay.out_trade_no")
//            ->field("wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, emp.admin_name, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time, ea.completed as exam_analysis_completed, ea.update_time as exam_analysis_update_time")
//                ->field("wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, emp.admin_name, ap.admin_name as processing_name, ad.admin_name as delivery_name, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time")
                ->field($field)
                ->select();
        }else{
            $list = $this->alias("wo")
                ->join("left join __PATIENT__ pt on pt.patient_id = wo.patient_id")
                ->join($examBasic)
                ->join($examBasicCP)
                ->join($examMeasure)
//            ->join("left join __EXAM_ANALYSIS__ ea on ea.exam_analysis_id = wo.exam_analysis_id")
                ->join("left join __ADMIN__ creator on creator.employee_id = wo.creator_id")
                ->join("left join __ADMIN__ sales on sales.employee_id = wo.sales_id")
                ->join("left join __ADMIN__ examinator on examinator.employee_id = wo.examinator_id")
                ->join("left join __ADMIN__ prescriber on prescriber.employee_id = wo.prescriber_id")
                ->join("left join __ADMIN__ reviewer on reviewer.employee_id = wo.reviewer_id")
                ->join("left join __ADMIN__ auditor on auditor.employee_id = wo.auditor_id")
                ->join("left join __ADMIN__ ap on ap.user_id = wo.processing_id")
                ->join("left join __ADMIN__ ad on ad.user_id = wo.delivery_id")
//            ->join("left join __PAY__ pay on pay.pay_id = wo.pay_id")
                ->where(getWhereSqlByMap($map))
                ->order("wo.create_time desc")
                ->limit($page->firstRow . ',' . $page->listRows)
//            ->field("wo.*,cus.nickname,p.product_name,cus.doctor_name,cus.hospital_name,pay.out_trade_no")
//            ->field("wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, emp.admin_name, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time, ea.completed as exam_analysis_completed, ea.update_time as exam_analysis_update_time")
//                ->field("wo.*, pt.patient_name, pt.sex, pt.age, pt.diagnosis, emp.admin_name, ap.admin_name as processing_name, ad.admin_name as delivery_name, pt.completed as patient_completed, eb.completed as exam_basic_completed, eb.update_time as exam_basic_update_time, em.completed as exam_measure_completed, em.update_time as exam_measure_update_time")
                ->field($field)
                ->select();
        }


        return $list;

    }

    public function getListCount($map = null, $map2) {

        $count = $this->alias("wo")
//            ->join("left join __PRODUCT__ p on p.product_id = wo.product_id")
//            ->join("left join __CUSTOMER__ cus on cus.customer_id = wo.customer_id")
            ->where(getWhereSqlByMap($map))
            ->where($map2)
            ->count();

        return $count;
    }

}

